package com.buyingtickets.controller;

import com.buyingtickets.dao.SiteDao;
import com.buyingtickets.dao.UnitIntervalDao;
import com.buyingtickets.entity.*;
import com.buyingtickets.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AdminController {
    @Autowired
    private TypeService typeService;
    @Autowired
    private UnitIntervalBiz unitIntervalBiz;
    @Autowired
    private TrainNumberService trainNumberService;
    @Resource
    private TypeBiz typeBiz;
@Resource
private AdminPowerBiz adminPowerBiz;
    @RequestMapping("maneger")
    public String toadmin() {
        return "admin";
    }

    @RequestMapping("route/toAdminTrain")
    public String toaddtrain(Model model) {
        List<Type> types = typeService.queryByCalss("车次类型");
        model.addAttribute("tntypes", types);
        List<UnitInterval> unitIntervals = unitIntervalBiz.queryAll();
        model.addAttribute("sites", unitIntervals);
        return "admin_region_addtrain";
    }

    //前往路线添加界面，显示车次表
    @RequestMapping("route/{page}/toAdminTrainnumber")
    public String addRoute(@PathVariable("page") Integer page, Model model) {
        int count = trainNumberService.queryAllCount("");
        pageUitl(page, count, "", model);
        model.addAttribute("pageFlag", 1);
        return "admin_trainnumber";
    }

    //模糊查询列车,翻页
    @RequestMapping("route/{tid}/{page}/querytrainnumberbyid")
    public String queryTrainByIDOrName(@Nullable @PathVariable("tid") String tid, @PathVariable("page") String page, Model model) {
        int page1 = 0;
        model.addAttribute("pageFlag", 0);
        if ("<><>".equals(tid)) {
            tid = "";
        }
        try {
            page1 = Integer.valueOf(page);
        } catch (Exception e) {
            e.printStackTrace();
            int count = trainNumberService.queryAllCount("");
            pageUitl(1, count, tid, model);
            return "admin_trainnumber";
        }
        if (StringUtils.hasText(tid)) {
            int count = trainNumberService.queryAllCount(tid);
            List<TrainNumber> trainNumbers = trainNumberService.queryPage(page1, tid);
            int size = trainNumbers.size();
            int pages = count % 5 == 0 ? (count / 5) : (count / 5 + 1);
            trainUitl(1, count, pages, size, trainNumbers, tid, model);
        } else {
            int count = trainNumberService.queryAllCount("");
            pageUitl(1, count, tid, model);
        }

        return "admin_trainnumber";
    }

    @RequestMapping("route/{page}/toAdminnumber")
    public String toAdminRegion(@PathVariable("page") Integer page, Model model) {//显示车次表
        int count = trainNumberService.queryAllCount("");
        pageUitl(page, count, "", model);
        model.addAttribute("pageFlag", 1);//判断网页跳转
        return "admin_trainnumber";
    }


    private void pageUitl(int page, int count, String query, Model model) {
        List<TrainNumber> trainNumbers = trainNumberService.queryPage(page, query);
        int size = trainNumbers.size();
        int pages = count % 5 == 0 ? (count / 5) : (count / 5 + 1);
        trainUitl(page, count, pages, size, trainNumbers, query, model);
    }

    private void trainUitl(int page, int count, int pages, int size, List<TrainNumber> trainNumbers, String query, Model model) {
        model.addAttribute("cspage", page);//当前页
        model.addAttribute("trainnumsList", trainNumbers);//车次集合
        model.addAttribute("trainsListSize", size);//单页条数
        model.addAttribute("trainsCount", count);//总条数
        model.addAttribute("trainPages", pages);//总页数
        model.addAttribute("trainquery", query);//模糊查询字符串
    }

    @RequestMapping("admin_update/{id}")
    public String admin_update(@PathVariable int id, Model model) {
        User user = adminPowerBiz.queryByID(id);
        List<Type> types = typeBiz.queryByCalss("用户类型");
        List<Type> zhengjianList = typeBiz.queryByCalss("证件类型");
        List<Type> xingbieList = typeBiz.queryByCalss("性别类型");
        List<Type> chengkeList = typeBiz.queryByCalss("乘客类型");
        model.addAttribute("user", user);
        model.addAttribute("zhengjianList", zhengjianList);
        model.addAttribute("xingbieList", xingbieList);
        model.addAttribute("chengkeList", chengkeList);
        model.addAttribute("types", types);
        return "admin_update";
    }

    @RequestMapping(value = "updateadmin", method = RequestMethod.POST)
    @ResponseBody
    public int update(HttpSession session, User user) {
        System.out.println(user);
        session.setAttribute("Admin", user);
        return adminPowerBiz.updateadmin(user);
    }

}